package arrAndstr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
    530. 二叉搜索树的最小绝对差
    二叉搜索树中序遍历得到的数组是有序的！！！！！！
        优化的方式：在每次携带前一个节点的val，这样就不用存储了
 */
public class T530 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    public static int getMinimumDifference(TreeNode root) {
        List<Integer> valueList = new ArrayList<>();
        getValueList(root, valueList);

        int result = Integer.MAX_VALUE;
        for (int i = 1; i < valueList.size(); i++) {
            result = Math.min(result, valueList.get(i)-valueList.get(i-1));
        }

        return result;
    }

    private static void getValueList(TreeNode root, List<Integer> valueList) {
        if (root == null)   return;

        getValueList(root.left, valueList);
        valueList.add(root.val);
        getValueList(root.right, valueList);
    }


    public static void main(String[] args) {
        TreeNode node = new TreeNode(1);
        node.right = new TreeNode(3);
        node.right.left = new TreeNode(2);

        System.out.println(getMinimumDifference(node));
    }
}
